package communication.thirdLayer;
import communication.secondLayer.*;
import communication.*;
import kernel.*;
import objects.Station;

import java.util.*;
import java.io.Serializable;

/**
 * @author Equipe Simpack 2005
 * Class that must implement any ThirdLayer protocol coded by an user.
 * 
 * 
 * @author bbrison
 * Cleaning and adding necessary stuff through this f***ing home made code.
 */
public abstract class ThirdLayerProtocol implements Serializable {
	
	
    public final static String AODV = "AODV";
    public final static String OLSR = "OLSR";
	
    /**
     * the current simulator 
     */	 
    protected Simulator mySim; //the simulator
    /**
     * vector of all the routers in the network
     */
    protected Vector<Router> network;
    
    /**
     * The protocol used
     */
    protected String _protocol;
    
    /**
     * New constructor permitting the implementation of differents protocols. 
     * The specific treatment treatment with the vector network must be done in 
     * the specialized class, the second constructor already making the treatment 
     * for a standard simulation.
     * @param sim the simulator
     * @param protocol the protocol
     */
    public ThirdLayerProtocol(Simulator sim, String protocol) {
	mySim = sim;
	network = new Vector<Router>();
	_protocol = protocol;
    }
	
    /**
     * constructor of the class
     * @param sim The simulator
     */
    public ThirdLayerProtocol(Simulator sim) {
	mySim = sim;
	this.network = new Vector<Router>();

	if(mySim.getStation().size()>0){
	    for(int i=0;i<sim.getStation().size();i++){
		this.network.add(((Station)mySim.getStation().get(i)).getRouter());
		System.out.println(((Station)mySim.getStation().get(i)).getRouter());
	    }
	}
    }
	
	
    /**
     * 
     * @return a vector containing all the routers in the network at the moment of calling
     */
    public Vector<Router> getNetwork() {
		
	Vector<Router> v=new Vector<Router>();
	if(this.getMySim().getStation().size()>0){
	    for(int i=0;i<this.getMySim().getStation().size();i++){
		v.add(((Station)this.getMySim().getStation().get(i)).getRouter());
	    }
	}
	return v;
    }

    /**
     * Oblige all the third layer protocol to define its time to say hello
     * @return
     */
    public abstract double getHelloTime();


    /**
     * 
     * @return the maximum size per frame 
     */
    public int frameDataMaxSize(){
	return this.getMySim().getCommunicationManager().getProtocole().sizePerFrame();
    }

    /**

    }
    /**
    * @return the current simulator
    */
    public Simulator getMySim() {
	return mySim;
    }

    /**
     * @param simulator
     */
    public void setMySim(Simulator simulator) {
	mySim = simulator;
    }
	
    /**
     * treatment of all the packages that have just arrived
     *
     */
    public abstract void packagesTreatment();
    /**
     *time for all the stations to say hello.
     */
    public abstract void routesMaintenance();
    /**
     * Treatment of the protocol
     * @param we
     */
    public abstract void failedCommunicationTreatment(WishedEmission we);
	

    /**
     * (non-Javadoc)
     * 
     * @see Results.Dumpable#xmlDump()
     */
    public String xmlDump() {
	return "<protocole name =\"" + getProtocoleName() + "\" /> ";
    }
	
    /**
     * Give the name of the protocol
     * 
     * @return the name
     * 
     * @uml.property name="protocoleName"
     */
    public abstract String getProtocoleName();

}
